package sample.stock;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class AdvanceYChart extends ExpandableListActivity {
	private static final String indicators[] = {
		  "Indicator 1",
		  "Indicator 2"
		};

	private	static final String types[][] = {
		  {
			  "Bollinger_Bands", "b",
			  "Parabolic_SAR", "p",
			  "Splits", "s",
			  "Volume", "v"
		  },
		  {
			  "MACD", "m26-12-9",
			  "MFI", "f14",
			  "ROC", "p12",
			  "RSI", "r14",
			  "Slow_Stoch", "ss",
			  "Fast_Stoch", "fs",
			  "Vol_MA", "vm",
			  "W_R", "w14"
		  }
	};
	
	private EditText symbol = null;
	private EditText IDs = null;
	private Spinner size = null;
	private Spinner time = null;
	private Spinner type = null;
	private Spinner scale = null;
	private Spinner movAvg = null;
	private Spinner expAvg = null;
	private Button viewChart = null;
	
	private String opt_size;
	private String opt_time;
	private String opt_type;
	private String opt_scale;
	private String opt_movAvg;
	private String opt_expAvg;
	@SuppressWarnings("unchecked")
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.advancechart);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        symbol = (EditText)findViewById(R.id.symbol);
        IDs = (EditText)findViewById(R.id.IDs);
        size = (Spinner)findViewById(R.id.size);
        time = (Spinner)findViewById(R.id.time);
        type = (Spinner)findViewById(R.id.type);
        scale = (Spinner)findViewById(R.id.scale);
        movAvg = (Spinner)findViewById(R.id.movAvg);
        expAvg = (Spinner)findViewById(R.id.expAvg);
    
        ArrayAdapter adapter = ArrayAdapter.createFromResource(
                this, R.array.sizes, android.R.layout.simple_spinner_item);
        size.setAdapter(adapter);
        size.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				switch(size.getSelectedItemPosition()){
		        case 1:
		        	opt_size = "t";
		        	break;
		        case 0: case 2: case 3: default:
		        	opt_size = "z";
		        	break;
		        }
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();
			}
		});
        
        adapter = ArrayAdapter.createFromResource(
                this, R.array.time_span, android.R.layout.simple_spinner_item);
        time.setAdapter(adapter);
        time.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				switch(time.getSelectedItemPosition()){
				case 1:
		        	opt_time = "1d";
		        	break;
		        case 0: case 2: default:
		        	opt_time = "5d";
		        	break;
		        case 3:
		        	opt_time = "3m";
		        	break;
		        case 4:
		        	opt_time = "6m";
		        	break;
		        case 5:
		        	opt_time = "1y";
		        	break;
		        case 6:
		        	opt_time = "2y";
		        	break;
		        case 7:
		        	opt_time = "5y";
		        	break;
		        case 8:
		        	opt_time = "my";
		        	break;
		        }
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();
			}
		});
        
        adapter = ArrayAdapter.createFromResource(
                this, R.array.types, android.R.layout.simple_spinner_item);
        type.setAdapter(adapter);
        type.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				switch(type.getSelectedItemPosition()){
				case 0: case 1: default: 
		        	opt_type = "&l"; 
		        	break;
		        case 2:
		        	opt_type = "b";
		        	break;
		        case 3:
		        	opt_time = "c";
		        	break;
		        }
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();
			}
		});
        
        adapter = ArrayAdapter.createFromResource(
                this, R.array.scales, android.R.layout.simple_spinner_item);
        scale.setAdapter(adapter);
        scale.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				switch(scale.getSelectedItemPosition()){
				case 0: case 2: default:
		        	opt_scale = "on";
		        	break;
		        case 1:
		        	opt_scale = "off";
		        	break;
		        }
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();
			}
		});
        
        adapter = ArrayAdapter.createFromResource(
                this, R.array.movingAvg, android.R.layout.simple_spinner_item);
        movAvg.setAdapter(adapter);
        movAvg.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				switch(movAvg.getSelectedItemPosition()){
				case 1:
		        	opt_movAvg = "m5";
		        	break;
		        case 2:
		        	opt_movAvg = "m10";
		        	break;
		        case 3:
		        	opt_movAvg = "m20";
		        	break;
		        case 4: case 0: default:
		        	opt_movAvg = "m50";
		        	break;
		        case 5:
		        	opt_movAvg = "m100";
		        	break;
		        case 6:
		        	opt_movAvg = "m200";
		        	break;
		        }
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();
			}
		});
        
        adapter = ArrayAdapter.createFromResource(
                this, R.array.exponentialAvg, android.R.layout.simple_spinner_item);
        expAvg.setAdapter(adapter);
        expAvg.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				switch(expAvg.getSelectedItemPosition()){
				case 1:
		        	opt_expAvg = "e5";
		        	break;
		        case 2:
		        	opt_expAvg = "e10";
		        	break;
		        case 3:
		        	opt_expAvg = "e20";
		        	break;
		        case 4: case 0: default:
		        	opt_expAvg = "e50";
		        	break;
		        case 5:
		        	opt_expAvg = "e100";
		        	break;
		        case 6:
		        	opt_expAvg = "e200";
		        	break;
		        }
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Toast.makeText(getApplicationContext(), "Nothing selected", Toast.LENGTH_SHORT).show();
			}
		});
        
        SimpleExpandableListAdapter expListAdapter =
			new SimpleExpandableListAdapter(
				this,
				createGroupList(),	// groupData describes the first-level entries
				R.layout.advance_firstchild,	// Layout for the first-level entries
				new String[] { "indicatorName" },	// Key in the groupData maps to display
				new int[] { R.id.childname },		// Data under "colorName" key goes into this TextView
				createChildList(),	// childData describes second-level entries
				R.layout.advance_secondchild,	// Layout for second-level entries
				new String[] { "typeName", "code" },	// Keys in childData maps to display
				new int[] { R.id.childname, R.id.code }	// Data under the keys above go into these TextViews
			);
		setListAdapter( expListAdapter );
		
		viewChart = (Button)findViewById(R.id.button1);
		viewChart.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent chartView = new Intent(AdvanceYChart.this, ChartView.class);
				chartView.putExtra("symbol", symbol.getText().toString());
				chartView.putExtra("IDs", IDs.getText().toString());
				chartView.putExtra("size", opt_size); // item position
				chartView.putExtra("time", opt_time);
				chartView.putExtra("type", opt_type);
				chartView.putExtra("scale", opt_scale);
				chartView.putExtra("movAvg", opt_movAvg);
				chartView.putExtra("expAvg", opt_expAvg);
				startActivity(chartView);
			}
		});
    }
	public void  onContentChanged  () {
        super.onContentChanged();
    }

    public boolean onChildClick(
            ExpandableListView parent, 
            View v, 
            int groupPosition,
            int childPosition,
            long id) {
        CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
        if( cb != null )
            cb.toggle();
        return false;
    }

    public void  onGroupExpand  (int groupPosition) {
    }

	@SuppressWarnings("unchecked")
	private List createGroupList() {
	  ArrayList result = new ArrayList();
	  for( int i = 0 ; i < indicators.length ; ++i ) {
		HashMap m = new HashMap();
	    m.put( "indicatorName",indicators[i] );
		result.add( m );
	  }
	  return (List)result;
    }

  @SuppressWarnings("unchecked")
private List createChildList() {
	ArrayList result = new ArrayList();
	for( int i = 0 ; i < types.length ; ++i ) {
// Second-level lists
	  ArrayList secList = new ArrayList();
	  for( int n = 0 ; n < types[i].length ; n += 2 ) {
	    HashMap child = new HashMap();
		child.put( "typeName", types[i][n] );
	    child.put( "code", types[i][n+1] );
		secList.add( child );
	  }
	  result.add( secList );
	}
	return result;
  }
}
